package org.vaadinetherealintegration.client.example3;

import org.vaadinetherealintegration.client.ExampleInterface;

import etrl.core.Group;
import etrl.core.LightStore;
import etrl.core.Lights;
import etrl.core.Node;
import etrl.core.Viewer;
import etrl.resources.Resources;
import etrl.vecmath.Quat4f;
import etrl.vecmath.Vector3f;

public class Example3 implements ExampleInterface
{
	private Viewer _viewer;
	private Picker _picker;
	
	String stone_wall = "VAADIN/widgetsets/org.vaadinetherealintegration.MyWidgetSet/stone_wall.bmp";
    String floor = "VAADIN/widgetsets/org.vaadinetherealintegration.MyWidgetSet/floor.jpg";
    
	public Example3(Viewer viewer)
	{
		_viewer = viewer;
		_picker = null;
	}
	
	
	public Node buildSceneRoot()
	{
		Group root = new Group();
		root.setName("root");
		
		Map map = new Map(10, 10.f);
		map.addBlock(4, 5);
		map.addBlock(6, 5);
		
		// Add lights
		LightStore.getInstance().createLight("FixedLight0");
		LightStore.getInstance().setLightPositionDirection("FixedLight0", new Vector3f(5.f, 20.f, 20.f));
		LightStore.getInstance().setLightColor("FixedLight0", new Vector3f(0.8f, 0.8f, 0.8f));
		
		Lights lights = new Lights();
		lights.addLight("FixedLight0");
		lights.addChild(map.getGraph());
		lights.setName("lights");
		
		root.addChild(lights);

		_picker = new Picker(_viewer, root);
		_viewer.registerListener(_picker);
		
		return root;
	}

	
	public Vector3f getInitialCameraPos()
	{
		return new Vector3f(5.f, 10.f, 40.f);
	}

	
	public Quat4f getInitialCameraOrientation()
	{
		return new Quat4f(0.f, 0.f, 0.f, 1.f);
	}

	
	public void atExit()
	{
		_viewer.unregisterListener(_picker);
		_picker = null;
		
		// clear example resources
		_viewer.unloadResources();
	}

	
	public void atStart()
	{
		// Load example texture resources
		Resources.instance().addTextureToLoad(stone_wall, true);
		Resources.instance().addTextureToLoad(floor, true);
		_viewer.loadResources();		
	}

}
